package parkshop.pmcourse.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import parkshop.pmcourse.domain.Customer;
import parkshop.pmcourse.domain.MallAdministrator;
import parkshop.pmcourse.domain.Shop;
import parkshop.pmcourse.dto.User;
import parkshop.pmcourse.mapping.CustomerMapper;
import parkshop.pmcourse.mapping.ShopMapper;
import parkshop.pmcourse.service.CustomerService;
import parkshop.pmcourse.utils.ConstantUtil;

@Service
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerMapper customerMapper;
	@Autowired
	private ShopMapper shopMapper;
	
	@Override
	public User signin(String name, String password) {
		// TODO Auto-generated method stub
		Customer customer=customerMapper.selectByName(name);
		if(customer!=null && customer.getIsActived() == 1 && password.equals(customer.getPassword())&&(customer.getIssuspended()==null||ConstantUtil.wasBlackList!=customer.getIssuspended()))
			return new User(customer);
		return null;
	}

	@Override
	public boolean signup(User user) {
		// TODO Auto-generated method stub
		if(user==null)
			return false;
		if(null!=customerMapper.selectByName(user.getName()))
			return false;
		Customer customer=new Customer(user);
		customer.setIssuspended((byte) (1-ConstantUtil.wasBlackList));
		customer.setIsdeleted((byte) (1-ConstantUtil.wasBlackList));
		int rows=customerMapper.insertSelective(customer);
		return rows==1?true:false;
	}

	@Override
	public boolean openShop(User user, String shopName) {
		// TODO Auto-generated method stub
		Shop shop=new Shop();
		shop.setContacts(user.getName());
		//shop.setEmail(user.);
		shop.setIsapproved(Byte.valueOf((byte) 0));
		shop.setIsdelete(Byte.valueOf((byte) 1));
		shop.setIssuspended(Byte.valueOf((byte) 1));
		shop.setName(shopName);
		shop.setPhone(user.getPhone());
		shop.setShopOwnerId(user.getId());
		if(shopMapper.selectByShopOwnerId(user.getId())!=null)
			return false;
		
		int rows=shopMapper.insertSelective(shop);
		return rows==1?true:false;
	}

	@Override
	public boolean updatePersonInfo(User user) {
		// TODO Auto-generated method stub
		Customer customer=new Customer(user);
		int rows=customerMapper.updateByPrimaryKeySelective(customer);
		return rows==1?true:false;
	}

	@Override
	public boolean activeUser(String code) {
		Customer customer = customerMapper.getCustomerByActiveCode(code);
		if (customer == null) {
			return false;
		}
		customer.setIsActived((byte)1);
		int rows = customerMapper.updateCustomerByActiveCode(customer);
		return rows==1?true:false;
	}

}
